package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private StrategyTagMapper strategyTagMapper;
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;

    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public StrategyDetail getById(Long id) {
        StrategyDetail detail = strategyDetailMapper.selectByPrimaryKey(id);
        //查询文章内容
        StrategyContent content = strategyContentMapper.selectByPrimaryKey(id);
        detail.setStrategyContent(content);
        return detail;
    }

    public AjaxResult saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        AjaxResult result = new AjaxResult();
        try {
            //获得文章内容
            StrategyContent content = strategyDetail.getStrategyContent();

            //生成摘要, 模拟AI生成
            strategyDetail.setSummary("不关我的事,AI自动生成的");

            //连表分类表, 查询并填充攻略名称和目的地
            StrategyCatalog catalog = strategyCatalogMapper.selectByPrimaryKey(strategyDetail.getCatalog().getId());
            strategyDetail.setStrategyName(catalog.getStrategyName());
            Destination dest = new Destination();
            dest.setId(catalog.getDestId());
            strategyDetail.setDest(dest);

            if(strategyDetail.getId() == null){ //新增
                //设置创建时间
                strategyDetail.setCreateTime(new Date());
                //保存
                strategyDetailMapper.insert(strategyDetail);
                //保存文章
                content.setId(strategyDetail.getId());
                strategyContentMapper.insert(content);

                //每次新增加一篇攻略的时候, 把它放到redis缓存里面统计数据
                StrategyStats stats = new StrategyStats();
                stats.setStrategyId(content.getId());
                strategyStatsCacheService.setStrategyStats(stats);

            }else{
                //修改
                strategyDetailMapper.updateByPrimaryKey(strategyDetail);
                //修改文章表
                content.setId(strategyDetail.getId());
                strategyContentMapper.updateByPrimaryKey(content);
                //删除旧标签关系
                strategyTagMapper.deleteRelation(strategyDetail.getId());
            }
            //保存新标签关系
            if(tags != null){
                String[] ts = tags.split(",");
                for (String tagName : ts) {
                    //先查询数据库是否存在相同的标签
                    StrategyTag tag = strategyTagMapper.selectByName(tagName);
                    if (tag == null) { //没有相同的标签, 就保存新的标签
                        tag = new StrategyTag();
                        tag.setName(tagName);
                        strategyTagMapper.insert(tag);
                    }
                    //保存关系
                    strategyTagMapper.insertRelation(strategyDetail.getId(),tag.getId());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    public List<StrategyDetail> listViewnumTop3(Long destId) {
        return strategyDetailMapper.listViewnumTop3(destId);
    }

    public List<StrategyDetail> list() {
        return strategyDetailMapper.listStats();
    }

    public void updateDetail(StrategyDetail detail) {
        strategyDetailMapper.updateStatsNum(detail);
    }

    public List<StrategyDetail> listAll() {
        return strategyDetailMapper.selectAll();
    }
}
